﻿// -----------------------------------------------------------------------
// <copyright file="Globals.cs" company="Mar3ek">
// http://mar3ek.wordpress.com
// </copyright>
// -----------------------------------------------------------------------

using System.Globalization;

namespace LighthouseNET
{
    using System;
    using System.Runtime.InteropServices;
    using System.Security;

    /// <summary>
    /// Global application settings and methods
    /// </summary>
    internal static class Globals
    {
        /// <summary>
        /// Converts date a time to ISO.8601 format string
        /// </summary>
        /// <param name="date">Date to convert</param>
        /// <returns>String containing the date and time in ISO.8601 format</returns>
        internal static DateTime DateTimeFromLighthouse(string date)
        {
            //var splitRegex = new Regex(@"(?<year>[\d]{4})-(?<month>[\d]{2})-(?<day>[\d]{2})T(?<hour>[\d]{2}):(?<minute>[\d]{2}):(?<second>[\d]{2})");

            //if (splitRegex.IsMatch(date))
            //{
            //    var match = splitRegex.Match(date);
            //    var result = new DateTime(
            //        int.Parse(match.Groups["year"].Value),
            //        int.Parse(match.Groups["month"].Value),
            //        int.Parse(match.Groups["day"].Value),
            //        int.Parse(match.Groups["hour"].Value),
            //        int.Parse(match.Groups["minute"].Value),
            //        int.Parse(match.Groups["second"].Value));

            //    return result;
            //}

            DateTime result;
            if (date.EndsWith("Z"))
            {
                return DateTime.TryParseExact(date, "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture, DateTimeStyles.None, out result) ? result.ToLocalTime() : DateTime.Now;
            }

            return DateTime.TryParseExact(date, "yyyy-MM-ddTHH:mm:sszzz", CultureInfo.InvariantCulture, DateTimeStyles.None, out result) ? result : DateTime.Now;
        }

        /// <summary>
        /// Converts date and time data to the ISO.8601 format date String
        /// </summary>
        /// <param name="date">DateTime data to be converted</param>
        /// <returns>ISO.8601 compatible String</returns>
        internal static string DateTimeToLighthouse(DateTime date)
        {
            //var month = date.Month.ToString("D2");
            //var d = date.Day.ToString("D2");
            //var h = date.Hour.ToString("D2");
            //var m = date.Minute.ToString("D2");
            //var s = date.Second.ToString("D2");

            if (TimeZone.CurrentTimeZone.GetUtcOffset(date) == TimeSpan.Zero)
            {
                return date.ToString("yyyy-MM-ddTHH:mm:ssZ");
            }

            return date.ToString("yyyy-MM-ddTHH:mm:ssZzzz");

            //string.Format("{0}-{1}-{2}T{3}:{4}:{5}Z", date.Year, month, d, h, m, s);
        }

        /// <summary>
        /// Decrypts the specified secure string
        /// </summary>
        /// <param name="ss">The secure string to decrypt.</param>
        /// <returns>The decrypted string</returns>
        internal static string Dss(SecureString ss)
        {
            var ptr = Marshal.SecureStringToBSTR(ss);
            var decrypted = Marshal.PtrToStringUni(ptr);
            return decrypted;
        }
    }
}
